package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserInfoParam;
import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Travel;
import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.TravelQuery;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.service.ITravelService;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.util.UMEditorUploader;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/travels")
public class TravelController {

	@Autowired
	private ITravelService travelService;
	@Autowired
	private ITravelCommentService travelCommentService;

	@GetMapping("/query")
	public Object query(TravelQuery qo,@UserInfoParam UserInfo userInfo){
		qo.setStatus("yes");
		Page<Travel> page= travelService.query(qo,userInfo);
		return JsonResult.success(page);
	}

	@GetMapping("/detail")
	public Object detail(String id){
		return JsonResult.success(travelService.get(id));
	}

	@PostMapping("/saveOrUpdate")
	@RequireLogin
	public Object saveOrUpdate(Travel travel,@UserInfoParam UserInfo userInfo){
		travel.setUserId(userInfo.getId());
		travelService.saveOrUpdate(travel);
		return JsonResult.success(travel.getId());
	}

	@GetMapping("/getUser")
	public Object currentUser(@UserInfoParam UserInfo userInfo) {
		return JsonResult.success(userInfo);
	}

	@PostMapping("/commentAdd")
	@RequireLogin
	public Object commentAdd(TravelComment travelComment, @UserInfoParam UserInfo userInfo){
		BeanUtils.copyProperties(userInfo, travelComment);
		travelComment.setUserId(userInfo.getId());
		travelCommentService.addComment(travelComment);
		return JsonResult.success();
	}

	@GetMapping("/comments")
	public Object comments(String travelId){
		return JsonResult.success(travelCommentService.comments(travelId));
	}

	// 顶
	@PostMapping("/travelThumbup")
	@RequireLogin
	public Object travelThumbup(String tid,@UserInfoParam UserInfo userInfo){
		boolean ret = travelService.strategyThumbup(tid,userInfo.getId());
		return JsonResult.success(ret);
	}

	// 统计vo量
	@GetMapping("/travelsVo")
	public Object travelsVo(String tid){
		return JsonResult.success(travelService.travelsVo(tid));
	}

	// 收藏/取消
	@PostMapping("/favor")
	@RequireLogin
	public Object favor(String tid,@UserInfoParam UserInfo userInfo){
		boolean ret = travelService.travelFavor(tid,userInfo.getId());
		return JsonResult.success(ret);
	}

	//上传图片
	@PostMapping("/contentImage")
	public String uploadUEImage(MultipartFile upfile, HttpServletRequest request) throws Exception{
		UMEditorUploader up = new UMEditorUploader(request);
		String[] fileType = {".gif" , ".png" , ".jpg" , ".jpeg" , ".bmp"};
		up.setAllowFiles(fileType);
		up.setMaxSize(10000); //单位KB
		up.upload(upfile);

		String callback = request.getParameter("callback");
		String result = "{\"name\":\""+ up.getFileName() +"\", \"originalName\": \""+ up.getOriginalName() +"\", \"size\": "+ up.getSize()
				+", \"state\": \""+ up.getState() +"\", \"type\": \""+ up.getType() +"\", \"url\": \""+ up.getUrl() +"\"}";
		result = result.replaceAll( "\\\\", "\\\\" );
		if(callback == null ){
			return result ;
		}else{
			return "<script>"+ callback +"(" + result + ")</script>";
		}
	}

}
